<?php
/**
 *
 */
class Sx_friend_mod extends CI_Model {

	private $addfindTrue = FALSE;
	private $tab_user = "sx_users";
	private $fSelect = "ID,user_email,user_login,user_nicename,user_city,user_img";

	/*
	 * 添加好友 addFriend
	 * @access	public
	 * @param	$uid  用户ID 	string
	 * @param	$fid  好友ID 	string
	 * @return	bool
	 */
	function addFriend($uid, $fid) {
		//查询uid是否存在
		$this -> db -> where('friend_uid', $fid);
		$Q = $this -> db -> get('sx_friend');
		if ($Q -> num_rows() > 0) {
			//好友存在
			return TRUE;
		} else {
			$dArr = array("friend_uid" => $uid, "friend_fid" => $fid, "addTime" => date("Y-m-d H:i:s"));
			$this -> db -> insert('sx_friend', $dArr);
			if ($this -> db -> affected_rows() > 0) {
				//如果添加成功，返回为真
				return TRUE;
			} else {
				//如果添加失败，返回为假
				return FALSE;
			}
			$this -> db -> free_result();
		}
	}

	// ----------------------------------------------------------------
	/*
	 * 查找好友是否存在,查看用户所有好友
	 * @access	public
	 * @param	$uid  用户ID 	string
	 * @param	$fid  好友ID 	string
	 * @return	bool(好友存在为真，不存在为假)
	 */
	// ----------------------------------------------------------------
	function findFriend($uid, $fid = "", $all = FALSE) {
		//如果不是查看所有好友
		if ($all == FALSE) {
			$this -> db -> where('friend_uid', $uid);
			$this -> db -> where('friend_fid', $fid);
			$Q = $this -> db -> get('sx_friend');
			if ($Q -> num_rows() > 0) {
				$this -> addfindTrue = TRUE;
			}

			return $this -> addfindTrue;
		} else {
			//$all不为空，则查看所有好友
			$mArr = array();
			$this -> db -> where('friend_uid', $uid);
			if (is_array($all)) {
				$n = $this->fc01($uid);
				$all[1]<0 ?$all[1] = 0:$all[1];
				$all[1] >$n?$all[1] = $n-1:$all[1];
				$this -> db -> limit($all[0], $all[1]);
			}
			$Q = $this -> db -> get('sx_friend');
			if ($Q -> num_rows() > 0) {
				foreach ($Q->result_array() as $row) {
					$this -> db -> select($this -> fSelect);
					$this -> db -> where('ID', $row['friend_fid']);
					$Q1 = $this -> db -> get($this -> tab_user);
					$Y = $Q1 -> row_array();
					$Y["addFriendTime" ]=$row['addTime'];
					//此处需要修改
					$Y['CountFriend'] = $this->sx_db_mod -> get_CountFirend($row['friend_fid']);
            		$Y['CountArticle'] = $this->sx_db_mod -> get_CountArticle($row['friend_fid']);            
            		$Y['CountComment'] = $this->sx_db_mod -> get_Comment($row['friend_fid']);
            		//$time['Article'] = $this ->sx_db_mod-> get_Article($row['friend_fid'], NULL, NULL);					
					$mArr[] = $Y;
				}
				return $mArr;
			} else {
				//没有好友，返回-1
				return FALSE;
			}
		}

		$this -> db -> free_result();

	}

	// ----------------------------------------------------------------
	/*
	 * 查看好友一般信息
	 * @access	public
	 * @param	$uid  用户ID 	string
	 * @return	array(好友存在为真，不存在为假)
	 */
	// ----------------------------------------------------------------
	function selectFriend($uid) {
		$data = array();
		$this -> db -> select($this -> fSelect);
		$this -> db -> where('ID', $uid);
		$Q = $this -> db -> get($this -> tab_user);
		if ($Q -> num_rows() > 0) {

			$data = $Q -> row_array();

			return $data;
		} else {
			return FALSE;
		}

		$Q -> free_result();
	}

	// ----------------------------------------------------------------
	/*
	 * 搜索好友
	 * @access	public
	 * @param	$m  值 	string
	 * @param	$n  条件 	string
	 * @return	array(好友存在为真，不存在为假)
	 */
	// ----------------------------------------------------------------
	function searchFriend($m, $n) {
		//返回好友的信息数组
		$lyz = array();
		$this -> db -> where("$m", "$n");
		$this -> db -> select("ID,user_login,user_nicename,user_city,user_img");
		$this -> db -> from($this -> tab_user);
		$this -> db -> order_by("ID", "DESC");
		$Q = $this -> db -> get();
		if ($Q -> num_rows() > 0) {
			foreach ($Q->result_array() as $row) {
				$row['Online'] = "1";
				//关注
				$row['Attention'] = "1";
				//发布的帖子
				$row['Issue'] = "1";
				//被关注
				$row['BeAttented'] = "1";
				$lyz[] = $row;

			}

			//返回结果
			return $lyz;
		} else {
			//如果没有搜索到好友，返回false
			return FALSE;
		}
		$Q -> free_result();

	}

	// ----------------------------------------------------------------
	/*
	 * s删除好友
	 * @access	public
	 * @param	$uid  用户ID 	string
	 * @param	$fid  好友ID 	string
	 * @return	array 没有数据，返回假
	 */
	// ----------------------------------------------------------------
	function del_friend($uid, $fid) {
		$this -> tab_user = "sx_friend";
		$this -> db -> select("friend_uid,friend_fid");
		$this -> db -> where("friend_fid", "$fid");
		$this -> db -> from($this -> tab_user);
		$Q = $this -> db -> get();
		/*查询是否好友存在*/
		if ($Q -> num_rows() > 0) {
			$mArr = array("friend_fid" => "$fid", "friend_uid" => "$uid");
			$this -> db -> where($mArr);
			if ($this -> db -> delete($this -> tab_user)) {
				/*返回1，操作成功*/
				return "1";
			} else {
				//返回-1，删除失败
				return "-1";
			}
		} else {
			/*好友不存在，返回为假*/
			return FALSE;
		}
		$Q -> free_result();
	}
	
	/**
	 * fc01 
	 */
	function fc01($uid) {
			$this -> db -> where('friend_uid', $uid);
			$Q = $this -> db -> get('sx_friend');
			if($Q -> num_rows() > 0){
				return $Q -> num_rows();
			}else{
				return 0;
			}
			$Q -> free_result();
	}

}
?>